home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8614 / 8614.xpi / chrome / extension.jar / content / ui / recommendations / TeasersManager.js < prev   
Encoding:
JavaScript  |  2010-02-10  |  4.4 KB  |  144 lines

  1. Glydo.TeasersManager = Prototype.Class.create(Glydo.EventSource,{
  2.  
  3.     initialize: function($super,application) {
  4.         $super();
  5.         this.application = application;
  6.         this.localizedStrings = new Glydo.Utils.LocalizedBundle(document);
  7.         this.paneTitle = this.localizedStrings.get("vertical_pane.teaser.title");
  8.         this.anchor = document.getElementById("status-bar");
  9.         this.teaserRecs = null;
  10.         this.lastTeaserMillis = Date.now();
  11.     },
  12.  
  13.  
  14.     onCurrentDocumentChanged: function(docEntry) {
  15.         // Make sure the teaser is closed
  16.         this.application.detachedPopupsManager.closePopup('teaser',Glydo.DetachedPopupsManager.CLOSE_REASONS.CONTEXT_CHANGE);
  17.     },
  18.     
  19.     onCurrentProcessedDocumentChanged: function(docEntry) {
  20.         // Make sure the teaser is closed
  21.         this.application.detachedPopupsManager.closePopup('teaser',Glydo.DetachedPopupsManager.CLOSE_REASONS.CONTEXT_CHANGE);
  22.         this.update(docEntry);
  23.     },
  24.     
  25.     update: function(docEntry) {
  26.         if (!docEntry) {
  27.             return;
  28.         }
  29.  
  30.         // If another popup is open, return immediately
  31.         if (this.application.detachedPopupsManager.isPopupOpen()) {
  32.             return;
  33.         }
  34.         // Get the earliest time a teaser may be shown
  35.         var minTeaserIntervalMillis = Glydo.Prefs.teaser_min_interval_millis;
  36.         if (minTeaserIntervalMillis === null || minTeaserIntervalMillis < 0) {
  37.             return;
  38.         }
  39.         if (this.lastTeaserMillis !== null) {
  40.             var cur = Date.now();
  41.             if (cur < this.lastTeaserMillis + minTeaserIntervalMillis) {
  42.                 return;
  43.             }
  44.         }
  45.  
  46.         // We are ok to show a teaser in terms of time elapsed
  47.         // We now obtain the list of candidate recommendations for the teaser, filtering those
  48.         // that we have already shown rececntly.
  49.         var greatRecs = docEntry.selectRecommendations(Glydo.TeasersManager.TOP_RECOMMENDATION_PANE_DEFS.filter);
  50.         greatRecs = greatRecs.filter(function(rec) {
  51.             return Glydo.LOGGING_DB.getRecLastViewedInTeaserTime(rec.url) === null;
  52.         },this);
  53.         
  54.         // If there are no results, we don't show a teaser
  55.         if (greatRecs.length == 0) {
  56.             return;
  57.         }
  58.         
  59.         // Otherwise, we show the best great rec as a teaser
  60.         this.teaserRecs = [greatRecs[0]];
  61.         
  62.         var contentsType = (this.teaserRecs[0].contextRelation == "same_product_alternative_offer") ? "product_offers" : this.teaserRecs[0].contentType;
  63.         var title = this.localizedStrings.get("vertical_pane.teaser." + contentsType + ".title");
  64.         if (!title) {
  65.             title = this.paneTitle;
  66.         }
  67.         this.application.detachedPopupsManager.focusWindowOrTogglePopup({
  68.             name: "teaser",
  69.             contentsType: contentsType,
  70.             title: title,
  71.             filter: this.teaserRecs,
  72.             adFilter: this.selectAdFilter(this.teaserRecs[0])
  73.         },{
  74.             anchor: this.anchor,
  75.             position: 'before_end',
  76.             auto_hide: Glydo.Prefs.teaser_auto_hide_interval_millis,
  77.             realEstateKind: 'TEASER'
  78.         });
  79.         this.lastTeaserMillis = Date.now();
  80.     },
  81.  
  82.     selectAdFilter: function(rec) {
  83.         if (rec.contentType == 'product' || rec.contentType == 'ad') {
  84.             return null;
  85.         }
  86.         return Glydo.TeasersManager.TEASER_ADS_FILTER;
  87.     },
  88.     
  89.     onPopupClosed: function(name,reason,time_open_millis) {
  90.         if (name != "teaser") {
  91.             return;
  92.         }
  93.         // If the teaser was closed as a result of direct
  94.         // user interaction or if it was shown for enough time,
  95.         // mark the rec as shown in the ticker
  96.         if (reason.interaction || time_open_millis > Glydo.Prefs.teaser_min_open_time_for_ack_millis) {
  97.             this.teaserRecs.forEach(function(rec) {
  98.                 Glydo.LOGGING_DB.markRecAsViewedInTeaser(rec.url,rec.receivedTime);
  99.             },this);
  100.         }
  101.         this.teaserRecs = null;
  102.     },
  103.     
  104. });
  105.  
  106. Glydo.TeasersManager.TEASER_ADS_FILTER = ({
  107.         condition: {
  108.             contentType: ["ad","product"]
  109.         },
  110.         orderBy: "-relevanceScore",
  111.         limit: 20
  112. });
  113.  
  114. Glydo.TeasersManager.TEASER_CONTENT_TYPES = [
  115.   "rss", "info", "news", "video", "other", "twitter"
  116. ];
  117.  
  118. Glydo.TeasersManager.TOP_RECOMMENDATION_PANE_DEFS = ({
  119.     name: "teaser",
  120.     filter: {
  121.         condition: function (rec) {
  122.             var valid = rec.isHurray && rec.title;
  123.             if (rec.contextRelation == "related_content") {
  124.                 valid = valid &&
  125.                     rec.excerpt &&
  126.                     rec.thumbnails && 
  127.                     rec.thumbnails.length > 0;
  128.             } else if (rec.contextRelation == "same_product_alternative_offer"){
  129.                 valid = valid &&
  130.                     rec.origin &&
  131.                     rec.priceValue &&
  132.                     rec.contextItem &&
  133.                     rec.contextItem.annotation &&
  134.                     rec.contextItem.annotation.price &&
  135.                     rec.priceValue < rec.contextItem.annotation.price;
  136.             } else {
  137.                 valid = false;
  138.             } 
  139.             return valid;
  140.         },
  141.         orderBy: "-relevanceScore"
  142.     }
  143. });
  144.